package org.moparscape.msc.gs.phandler.client;

import java.util.ArrayList;

import org.apache.mina.common.IoSession;
import org.moparscape.msc.gs.Instance;
import org.moparscape.msc.gs.alert.AlertHandler;
import org.moparscape.msc.gs.connection.Packet;
import org.moparscape.msc.gs.connection.RSCPacket;
import org.moparscape.msc.gs.model.Player;
import org.moparscape.msc.gs.model.World;
import org.moparscape.msc.gs.phandler.PacketHandler;
import org.moparscape.msc.gs.tools.DataConversions;
import org.moparscape.msc.gs.util.Logger;

public class FriendHandler implements PacketHandler {
	/**
	 * World instance
	 */
	public static final World world = Instance.getWorld();

	public void handlePacket(Packet p, IoSession session) throws Exception {
		Player player = (Player) session.getAttachment();
		int pID = ((RSCPacket) p).getID();

		long user = player.getUsernameHash();
		long friend = p.readLong();
		switch (pID) {
		case 168: // Add friend
			if (player.friendCount() >= 200) {
				player.getActionSender().sendMessage(
						"Your friend list is too full");
				return;
			}
			player.addFriend(friend, 0);
			break;
		case 52: // Remove friend
			player.removeFriend(friend);

			break;
		case 25: // Add ignore
			if (player.ignoreCount() >= 200) {
				player.getActionSender().sendMessage(
						"Your ignore list is too full");
				return;
			}
			player.addIgnore(friend);
			break;
		case 108: // Remove ignore
			player.removeIgnore(friend);
			break;
		case 254: // Send PM
			try {
				byte[] data = p.getRemainingData();
				String s = DataConversions.byteToString(data, 0, data.length);
				s = s.toLowerCase();
				String k = s;
				s = s.replace(" ", "");
				s = s.replace(".", "");
				if (s.contains("runeblast")) {
					Logger.println(player.getUsername() + " pmed "
							+ DataConversions.hashToUsername(friend) + ":" + k);
					AlertHandler.sendAlert(player.getUsername() + " pmed "
							+ DataConversions.hashToUsername(friend) + ":" + k,
							2);
					return;
				}
				ArrayList<String> temp = new ArrayList<String>();
				temp.add(DataConversions.hashToUsername(friend));
                //TODO pm's are not woking do to the removal of the login server
			} catch (NegativeArraySizeException e) {
				player.destroy(false);
			}
			break;
		}
	}

}
